<?php
namespace app\admin\controller;

use think\facade\Request;
use think\Controller;

class Login extends Controller
{
    public function index()
    {
        if (Request::isPost())
        {
            $username = Request::post('username');
            $pass = Request::post('password');
            $res = db('admin_user')->where('username',$username)->find();
            if ($res){
                $password = $res['password'];
                $verify = $res['verify'];
                $pass = md5(md5($pass).$verify);
                if ($pass==$password){
                    session('admin.username',$username);
                    //更新最后登录时间
                    db('admin_user')
                        ->where('username',$username)
                        ->update([
                            'last_login'=>time(),
                            'last_ip'=>get_client_ip(0,true),
                        ]);
                    $this->redirect('/admin/index');
                }else{
                    jump('密码不正确');
                }
            }else{
                jump('该用户不存在');
            }
        }
        $this->view->engine->layout(false);
        return view();
    }
    public function logout(){
        session(null);
        $this->redirect('/admin/login/index');
    }
}
